<?php
define('FPDF_FONTPATH','font/');
require('../apps/fpdf.php');
include_once "../apps/ManagerIUDS.php";

class PDF extends FPDF{
function FancyTable($predicateId,$medicalCareId,$maintenanceDate,$petId){
    // Colors, line width and bold font
	$managerIUDS = new ManagerIUDS();
	//Title
	$this->SetFont('tahoma','',10);
    $this->SetX(50);
    $this->Text( 100 , 10 , 'Invoice');
    //end title
    $petData = $managerIUDS->queryData("pet_name","pet"," WHERE pet_id='".$petId."'","1","");		

    //pet name
	$this->SetFont('tahoma','',10);
    $this->Text( 50 , 20 , iconv( 'UTF-8','cp874' ,'ชื่อสัตว์ : '));
    $this->Text( 65 , 20 , $petData["pet_name"]);
    //end pet name
	$w = array(25, 40, 25, 25);
    // Data
	$dataList = $managerIUDS->queryData("*","history"," WHERE history_type='M' AND maintenance_date='".$maintenanceDate."' AND history_id='".$medicalCareId."'","2","");
	$predicateDataList = $managerIUDS->queryData("*","history"," WHERE history_type='P' AND predicate_date = '".$maintenanceDate."' AND history_id='".$predicateId."'","2","");
	$i=0;
	$sum = 0;   
	//================== Predicate ================== 
	if(sizeof($predicateDataList)>0){
		$header = array('ลำดับ', 'รายการ', 'จำนวน', 'ราคา');
		//sub title
		$this->SetY(25);
		$this->SetX(50);
		$this->Cell( 50 , 7 , iconv( 'UTF-8','cp874' ,'รายการวินิจฉัย'),0,1);
	    //End sub title
		//$predicateHeader = array('ลำดับ', 'รายการ', 'จำนวน', 'ราคา','ราคารวม');
		$predicateHeader = array('ลำดับ', 'รายการ', 'จำนวน', 'ราคา');
		$width = array(25, 40, 25, 25);
		$this->SetX(50);
		for($i=0;$i<count($predicateHeader);$i++){
			$this->Cell($width[$i],7,iconv( 'UTF-8','cp874' ,$predicateHeader[$i]),1,0,'C');
	 	}
	    $this->Ln();
		foreach($predicateDataList as $value){
				$predicateData=$value["predicate_data"];
				$mySplit = explode(", ", $predicateData);
				for($j=0;$j<sizeof($mySplit);$j++){
					$predicateList = $managerIUDS->queryData("predicate_name,price","predicate"," WHERE predicate_id='".trim($mySplit[$j])."'","1","");		
		    		$this->SetX(50);
		        	$this->Cell($w[0],6,($j+1),1,0,'C');
		        	$this->Cell($w[1],6,iconv( 'UTF-8','cp874' ,$predicateList["predicate_name"]),1,0,'C');
		        	$this->Cell($w[2],6,"-",1,0,'R',$fill);
		        	$this->Cell($w[3],6,iconv( 'UTF-8','cp874' ,number_format($predicateList["price"])),1,0,'R');
		        	$this->Ln();
		        	$sum += $predicateList["price"];
			}
		}
	    $this->SetX(50);
	    $this->Cell(90,6,iconv( 'UTF-8','cp874' ,'ราคารวม'),1,0,'R');
	    $this->Cell(25,6,iconv( 'UTF-8','cp874' ,number_format($sum)),1,0,'R');
	    $this->Ln();
	}
	//================== End Predicate ================== 
	    
	//================== Medical Care ================== 
	if(sizeof($dataList)>0){
		$medicalCarePrice = 0;
		$header = array('ลำดับ', 'รายการ', 'จำนวน', 'ราคา');
	   //sub title
		$this->SetFont('tahoma','',10);
	   // $this->Text( 50 , 30 , iconv( 'UTF-8','cp874' ,'รายการรักษา'));
	    if(sizeof($predicateDataList)<=0){
			$this->SetY(25);
	    }
	    $this->SetX(50);
	   	$this->Cell( 50 , 6 , iconv( 'UTF-8','cp874' ,'รายการรักษา'),0,1);
	    //End sub title
	    $this->SetX(50);
	    $this->SetFillColor(0,0,0);
	    $this->SetTextColor(0);
	    $this->SetDrawColor(0,0,0);
	    $this->SetLineWidth(.3);
	   // $this->SetFont('','B');
		$this->SetFont('tahoma','',10);
	    // Header
	    for($i=0;$i<count($header);$i++){
	        $this->Cell($w[$i],7,iconv( 'UTF-8','cp874' ,$header[$i]),1,0,'C');
	    }
	    $this->Ln();
	    // Color and font restoration
	    $this->SetFillColor(255,255,255);
	    $this->SetTextColor(0);
		$this->SetFont('tahoma','',10);
		foreach($dataList as $value){
			$medicalId=$value["medical_care_data"];
			$mySplit = explode(", ", $medicalId);
			for($j=0;$j<sizeof($mySplit);$j++){
				$medicalCareList = $managerIUDS->queryData("medical_care_name,price","medical_care"," WHERE medical_care_id='".trim($mySplit[$j])."'","1","");		
	    		$this->SetX(50);
	        	$this->Cell($w[0],6,($j+1),1,0,'C');
	        	$this->Cell($w[1],6,iconv( 'UTF-8','cp874' ,$medicalCareList["medical_care_name"]),1,0,'C');
	        	$this->Cell($w[2],6,"-",1,0,'R',$fill);
	        	$this->Cell($w[3],6,iconv( 'UTF-8','cp874' ,number_format($medicalCareList["price"])),1,0,'R');
	        	$this->Ln();
	        	$medicalCarePrice += $medicalCareList["price"];
			}
		}
	    // Closing line
	    $this->SetX(50);
	    $this->Cell(90,6,iconv( 'UTF-8','cp874' ,'ราคารวม'),1,0,'R');
	    $this->Cell(25,6,iconv( 'UTF-8','cp874' ,number_format($medicalCarePrice)),1,0,'R');
	    $this->Ln();
	    
	    $this->SetX(50);
	    $this->Cell( 50 , 6 , iconv( 'UTF-8','cp874' ,'รายการค่ายา'),0,1);
        	    //End sub title
	    $this->SetX(50);
	    $this->SetFillColor(0,0,0);
	    $this->SetTextColor(0);
	    $this->SetDrawColor(0,0,0);
	    $this->SetLineWidth(.3);
	   // $this->SetFont('','B');
		$this->SetFont('tahoma','',10);
	    // Header
		$width = array(10, 40, 20, 20, 25);
	    $medicineHeader = array('ลำดับ', 'รายการ', 'จำนวน', 'ราคา','ราคารวม');
	    for($i=0;$i<count($medicineHeader);$i++){
	        $this->Cell($width[$i],7,iconv( 'UTF-8','cp874' ,$medicineHeader[$i]),1,0,'C');
	    }
	    $this->Ln();
	    // Color and font restoration
	    $this->SetFillColor(255,255,255);
	    $this->SetTextColor(0);
		$this->SetFont('tahoma','',10);
		$medicinePrice = 0;
		foreach($dataList as $value){
			$medicineId=$value["medicine_data"];
			$buffer = explode(", ", $medicineId);
			for($j=0;$j<sizeof($buffer);$j++){
				$mySplit =  explode(":", $buffer[$j]);
				$medicineList = $managerIUDS->queryData("medicine_name,price","medicine"," WHERE medicine_id='".trim($mySplit[0])."'","1","");		
	    		$this->SetX(50);
	        	$this->Cell($width[0],6,($j+1),1,0,'C');
	        	$this->Cell($width[1],6,iconv( 'UTF-8','cp874' ,$medicineList["medicine_name"]),1,0,'C');
	        	$this->Cell($width[2],6,iconv( 'UTF-8','cp874' ,number_format($mySplit[1])),1,0,'R');
	        	$this->Cell($width[3],6,iconv( 'UTF-8','cp874' ,number_format($medicineList["price"])),1,0,'R');
	        	$this->Cell($width[4],6,iconv( 'UTF-8','cp874' ,number_format($medicineList["price"]*$mySplit[1])),1,0,'R');
	        	$this->Ln();
	        	$medicinePrice += ($medicineList["price"]*$mySplit[1]);
			}
		}
	    // Closing line
	    $this->SetX(50);
	    $this->Cell(90,6,iconv( 'UTF-8','cp874' ,'ราคารวม'),1,0,'R');
	    $this->Cell(25,6,iconv( 'UTF-8','cp874' ,number_format($medicinePrice)),1,0,'R');
	    $this->Ln();
		}
	    $this->SetX(50);
	    $this->Cell(90,6,iconv( 'UTF-8','cp874' ,'ราคารวมทั้งหมด'),0,0,'R');
	    $this->Cell(25,6,iconv( 'UTF-8','cp874' ,number_format($medicinePrice+$medicalCarePrice+$sum)),0,0,'R');
	    $this->Ln();
		
	//================== End Medical Care ================== 
	    
	if(sizeof($dataList)<=0 && sizeof($predicateDataList)<=0){
   	 $this->Text( 95 , 30 , iconv( 'UTF-8','cp874' ,'ไม่พบข้อมูลการรักษา'));
	}
}
}
$predicateId=$_GET["predicate_id"];
$medicalCareId=$_GET["medical_care_id"];
$maintenanceDate=$_GET["maintenance_date"];
$petId = $_GET["pet_id"];
$pdf = new PDF();
$pdf->AddFont('tahoma','','tahoma.php');
$pdf->AddPage();
$pdf->FancyTable($predicateId,$medicalCareId,$maintenanceDate,$petId);
$pdf->Output();
?>